﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace XWG.JWBST.CarInfoRegProcess
{
    //<summary>
    //车辆审核信息查询
    //</summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            string info = reader.ReadToEnd();
            string json = string.Empty;
            info = HttpUtility.UrlDecode(info);
            JObject oka = (JObject)JsonConvert.DeserializeObject(info);
            string id = oka["userID"].ToString().Replace("\"", string.Empty);
            string audit = oka["audit"].ToString().Replace("\"", string.Empty);
            List<CarAudit> carlist = new List<CarAudit>();
            List<XWG.JWBST.JWBSTEntity.TbBstCar> checkList = new List<JWBSTEntity.TbBstCar>();
            var user = XWG.JWBST.JWBSTBLL.TbBstUserBLL.Load(id);

            //记录请求信息
            JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
            visit.Id = Guid.NewGuid().ToString();
            visit.Userid = id;
            visit.Time = DateTime.Now;
            visit.Modulename = "BreakRules";
            visit.Optionname = "车辆审核信息查询";
            visit.Other = oka.ToString();
            XWG.JWBST.JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

            if (user != null) //判断用户表中是否存在该用户
            {
                if (user.Deltag == 0)//判断用户表中该用户是否被删除
                {
                    if (!string.IsNullOrEmpty(audit))   
                    {
                        //根据审核条件查询
                        checkList = XWG.JWBST.JWBSTBLL.TbBstCarBLL.CheckCarByUserIDAndAudit(id, audit);
                        return ReturnJson(ref json, carlist, checkList);
                    }
                    else
                    {
                        //根据ID查询全部
                        checkList = XWG.JWBST.JWBSTBLL.TbBstCarBLL.CheckCarByUserID(id);
                        return ReturnJson(ref json, carlist, checkList);
                    }
                }
                return "该用户已被删除";
            }
            else
            {
                return "该用户为未注册手机用户";
            }

        }

        private static string ReturnJson(ref string json, List<CarAudit> carlist, List<XWG.JWBST.JWBSTEntity.TbBstCar> checkList)
        {
            for (int i = 0; i < checkList.Count; i++)
            {
                CarAudit cAudit = new CarAudit();
                cAudit.CAR_PKID = checkList[i].CarPkid;
                cAudit.CAR_CARLICENSE = checkList[i].CarCarlicense;
                cAudit.ISAUDIT = checkList[i].Isaudit;
                carlist.Add(cAudit);
            }
            json = JsonConvert.SerializeObject(carlist);
            return json;
        }
    }

    public class CarAudit
    {
        public string CAR_PKID { get; set; }
        public string CAR_CARLICENSE { get; set; }
        public string ISAUDIT { get; set; }
    }
}
